import GameData from "./GameData";

/**
 * @Author: 陈瑞鹏
 * @Date:   2025-01-20 10:57:28
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2025-01-20 11:41:59
 */
export default class ReportLogs {

  private static _instance: ReportLogs = null;

  public static get ins(): ReportLogs {
    if (!this._instance) {
      this._instance = new ReportLogs();
    }
    return this._instance;
  }

  public initSdk() {

    let url = "//sdk.dreamo100.top/dreamod1601307384.min.js";

    let script = document.createElement('script');

    script.src = url;

    script.type = "text/javascript";

    script.onload = this.loadScriptComplete;

    document.getElementsByTagName('head')[0].appendChild(script);

  }

  /**
   * 载入js完成回调
   */
  loadScriptComplete() {
    window['DMSearchLight'] = new window['D']({ url: 'https://searchlight.dreamo100.top', rootName: "qhtuanjianyouxi", requestType: 1, distance: 1000 });

    // 崩溃上报：
    window.onerror = function (message, source, lineno, colno, error) {
      window['DMSearchLight'].report({
        type: "event",
        name: "异常",
        guestId: location.href,
        message: message,
        source: source,
        lineno: lineno,
        colno: colno,
        error_message: error ? error.message : '',
        error_stack: error ? error.stack : ''
      });
      return false;
    };
    console.log("loadScriptComplete")
  }

  /**
   * 上报行为操作
   * @param type 
   * @param name 
   * @param guestId 
   * @param userId 
   * @param desc 
   * @param roomId 
   */
  behaviorReporting(name: string = "房间操作", desc: string = "", roomId: string = "") {
    console.log('behaviorReporting', name, desc, roomId);
    window['DMSearchLight'] && window['DMSearchLight'].report({
      type: "event",//必须字段，固定，字符串
      name, //必须字段，固定，字符串
      guestId: GameData.ins.username, //必须字段，字符串，随机字符串就行，也可以用户id
      userId: GameData.ins.username, //必须字段，字符串，uname
      time: new Date().getTime(),//必须字段，number
      url: window.location.href,
      desc,
      roomId
    });
  }
}